package com.xpec.c4.db.usertype;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.ms.db.usertype.BasicToString;
import com.xpec.c4.db.util.CodecTool;

/**
 * 将关注的好友SET转为字符串
 * @author zhongzhengkai
 *
 */
public class SetStringToString extends BasicToString {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7566704002715966500L;

	@Override
	public boolean equals(Object x, Object y) {

//		if (x == y)
//			return true;
//
//		if (x != null && y != null) {
//
//			return super.equals(x, y);
//		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected String assemble(Object obj) {
		Set<String> set = (Set<String>) obj;
		StringBuilder str = new StringBuilder();

		int len = set.size();
		str.append(len).append(SPLITTER);
		Iterator<String> it = set.iterator();
		while (it.hasNext()) {
			CodecTool.append(str, it.next(), SPLITTER);
		}
		if (str.length() > 0) {
			str.setLength(str.length() - 1);
		}
		return str.toString();
	}

	@Override
	protected Object parse(String value) {
		if (value == null) {
			return null;
		}
		String[] strs = CodecTool.split(value, SPLITTER_RE);
		Set<String> set=new HashSet<String>();
		int index=0;
		int len=Integer.valueOf(strs[index++]);
		for(int i=0;i<len;i++){
			set.add(strs[index++]);
		}
		return set;
	}

	@Override
	public Class<?> returnedClass() {
		return Set.class;
	}

	@Override
	public int[] sqlTypes() {
		return CLOB_TYPES;
	}

}
